using System;
using StructureMap.Graph;

namespace IoCBattle.Infrastructorrrr
{
    /// <summary>
    /// Lack of mixins is the only reason why this guy needs to derive 
    /// from defaultconvention scanner
    /// </summary>
    public static class ConventionHelper 
    {
        /// <summary>
        /// 'YourTypical' to 'IAmATotal' convention
        /// </summary>
        /// <param name="concreteType"></param>
        /// <param name="interfacePrefix"></param>
        /// <param name="concreteTypePrefix"></param>
        /// <returns>Interface to bind the concrete class to</returns>
        /// <example>
        /// Example: YourTypicalBollox will be bound to IAmATotalBollox
        /// </example>
        public static Type FindInterfaceForConcreteUsingPrefixes(Type concreteType, string interfacePrefix, 
            string concreteTypePrefix)
        {
            var name = concreteType.Name;
            if (!name.StartsWith(concreteTypePrefix)) return null;

            var commonNamePart = name.Substring(concreteTypePrefix.Length);

            string interfaceName = interfacePrefix + commonNamePart;
            
            Type[] interfaces = concreteType.GetInterfaces();
            return Array.Find(interfaces, t => t.Name == interfaceName);
        }
    }
}